<?xml version="1.0" encoding="UTF-8" ?>
<?arg urn:xmd-environment=Adoor.Metadata.XmdEnvironment, Adoor.Metadata ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:UML="href://org.omg/UML/1.3"
	xmlns:UML2="href://org.omg/UML/1.3"
	xmlns:typing="urn:xmd-typing"	
	xmlns:log="urn:log"
	xmlns:env="urn:xmd-environment"
	exclude-result-prefixes="UML log">

  <!-- used to move xmlns:typing namespace declaration up to the root node (is there a better way ?)-->
  <xsl:attribute-set name="dummy">
    <xsl:attribute name="typing:dummy"/>
    <xsl:attribute name="UML:dummy"/>
    <xsl:attribute name="UML2:dummy"/>
  </xsl:attribute-set>

  <!--<xsl:attribute-set name="UML">
	<xsl:attribute name="UML:dummy"/>
</xsl:attribute-set>-->


  <UMLDataType>
    <!--
	Will be replaced by the wizard 
	<xsl:template match="UML:DataType[@name='string']">System.String</xsl:template>
	-->
  </UMLDataType>

  <xsl:template name="AssemblyName">
    <xsl:param name="component"/>
    <xsl:param name="namespace"/>
    <xsl:value-of select="$component/@name"/>
    <!--<xsl:value-of select="$component"/>-->
  </xsl:template>

  <xsl:template name="ComponentName">
    <xsl:param name="component"/>
    <xsl:variable name="id" select="@xmi.id"/>
    <xsl:value-of select="concat('Adoor.DAL.',$component/@name)"/>
  </xsl:template>

  <xsl:template match="UML:DataType" mode="length">
    <xsl:variable name="name" select="translate(@name, '[]', '()')"/>
    <xsl:value-of select="substring-after(substring-before($name,')'),'(')"/>
  </xsl:template>

  <xsl:template match="UML:DataType">
    <!--<xsl:message terminate="yes">datatype <xsl:value-of select="@name"/> not defined</xsl:message>-->
    <xsl:value-of select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
  </xsl:template>

  <xsl:template match="/">
    <xsl:variable name="SetTotal" select="env:SetProgressTotal(count(/XMI/XMI.content/UML:Model//UML:Class[@name]) + count(/XMI/XMI.content/UML:Model//UML:Interface[@name]))" xmlns:env="urn:xmd-environment" />
    <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:Component[@name]"/>
  </xsl:template>

  <xsl:template match="UML:Component">
    <xsl:variable name="name" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="component" select="." />
    <xsl:processing-instruction name="file">
      <xsl:value-of select="$name"/>.pivot.xml
    </xsl:processing-instruction>
    <xsl:element name="{$name}" use-attribute-sets="dummy">
      <!--<xsl:element name="{$name}">-->
      <xsl:for-each select="UML:Component.residentElement/UML:ElementResidence/UML:ElementResidence.resident/UML:Class">
        <xsl:variable name="idref" select="@xmi.idref"/>
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:Class[@xmi.id=$idref and @name]">
          <xsl:with-param name="component" select="$component"/>
        </xsl:apply-templates>
      </xsl:for-each>
      <xsl:for-each select="UML:Component.residentElement/UML:ElementResidence/UML:ElementResidence.resident/UML:Interface">
        <xsl:variable name="idref" select="@xmi.idref"/>
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:Interface[@xmi.id=$idref and @name]">
          <xsl:with-param name="component" select="$component"/>
        </xsl:apply-templates>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template match="UML:Interface">
    <xsl:param name="component"/>
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="id" select="@xmi.id"/>
    <xsl:variable name="uuid" select="@xmi.uuid"/>
    <xsl:variable name="type">
      <xsl:apply-templates mode="FullType" select=".">
        <xsl:with-param name="component" select="$component"/>
      </xsl:apply-templates>
    </xsl:variable>

    <xsl:variable name="log" select="env:Log(concat('Processing Interface ', $qname),'0')" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>
    <xsl:element name="{$qname}">
      <typing:interface type="{$type}"/>
      <xsl:call-template name="Features">
        <xsl:with-param name="class" select="."/>
        <xsl:with-param name="inherited" select="false()"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <xsl:template match="UML:Interface" mode="ref">
    <xsl:param name="component"/>
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="id" select="@xmi.id"/>
    <xsl:variable name="uuid" select="@xmi.uuid"/>
    <xsl:variable name="type">
      <xsl:apply-templates mode="FullType" select=".">
        <xsl:with-param name="component" select="$component"/>
      </xsl:apply-templates>
    </xsl:variable>
    <typing:interfaces>
      <typing:interface type="{$type}" name="{$qname}"/>
    </typing:interfaces>
  </xsl:template>

  <xsl:template match="UML:Class">
    <xsl:param name ="component" />
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="log" select="env:Log(concat('Processing Class ', $qname),'0')" xmlns:env="urn:xmd-environment"/>
    <xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>

    <xsl:variable name="id" select="@xmi.id"/>
    <xsl:variable name="uuid" select="@xmi.uuid"/>
    <xsl:variable name="type">
      <xsl:apply-templates mode="FullType" select=".">
        <xsl:with-param name="component" select="$component"/>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="abstract" select="@isAbstract='true'"/>
    <xsl:variable name="genid" select="./UML:GeneralizableElement.generalization/UML:Generalization/@xmi.idref"/>
    
    <xsl:choose>
      <xsl:when test="$genid">
        <!-- Whe have a parent somewhere -->
        <xsl:variable name="parent" select="/XMI/XMI.content/UML:Generalization[@xmi.id = $genid]/UML:Generalization.parent/UML:Class/@xmi.idref"/>
        <xsl:variable name="parentNode" select ="/XMI/XMI.content/UML:Model//UML:Class[@xmi.id = $parent]"/>
        <xsl:variable name="parenttype" >
          <xsl:apply-templates mode="FullType" select="$parentNode">
            <xsl:with-param name="component" select="$component"/>
          </xsl:apply-templates>
        </xsl:variable>

        <xsl:element name="{$qname}">
          <xsl:choose>
            <!-- don't use <xsl:attribute> otherwise generated <typing:entity> is split into begin and end tags-->
            <xsl:when test="$abstract">
              <typing:entity type="{$type}" parent="{$parenttype}" parentEntity="{env:QName($parentNode/@name)}" abstract="true" uuid="{$uuid}"/>
            </xsl:when>
            <xsl:otherwise>
              <typing:entity type="{$type}" parent="{$parenttype}" parentEntity="{env:QName($parentNode/@name)}" uuid="{$uuid}"/>
            </xsl:otherwise>
          </xsl:choose>

          <xsl:call-template name="Features">
            <xsl:with-param name="class" select="."/>
            <xsl:with-param name="inherited" select="false()"/>
          </xsl:call-template>

        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <!-- No parent -->

        <xsl:element name="{$qname}">
          <xsl:choose>
            <!-- don't use <xsl:attribute> otherwise generated <typing:entity> is split into begin and end tags-->
            <xsl:when test="$abstract">
              <typing:entity type="{$type}" abstract="true" uuid="{$uuid}"/>
            </xsl:when>
            <xsl:otherwise>
              <typing:entity type="{$type}" uuid="{$uuid}"/>
            </xsl:otherwise>
          </xsl:choose>

          <xsl:call-template name="Features">
            <xsl:with-param name="class" select="."/>
            <xsl:with-param name="inherited" select="false()"/>
          </xsl:call-template>

        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
    


      <!--
      <xsl:for-each select="/XMI/XMI.content/UML:Model//UML:Abstraction[@client = $id]">
        <xsl:variable name="supplier" select="@supplier"/>
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:Interface[@xmi.id = $supplier]" mode="ref"/>
      </xsl:for-each>
-->
  </xsl:template>

  <xsl:template match="UML:Attribute">
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:element name="{$qname}">
      <xsl:variable name="idref" select="UML2:TypedElement.type/UML:DataType/@xmi.idref"/>
      <!--<xsl:choose>
        <xsl:when test="/XMI/XMI.content/UML:Model//UML:Class/@xmi.id=$idref">
          <xsl:variable name="type">
            <xsl:apply-templates mode="FullType" select="/XMI/XMI.content/UML:Model//UML:Class[@xmi.id=$idref]"/>
          </xsl:variable>
          <typing:reference type="{$type}" multiplicity="1" aggregate="true"/>
        </xsl:when>
        <xsl:otherwise>-->
      <xsl:variable name="datatypenode" select="/XMI/XMI.content/UML:Model//UML:DataType[@xmi.id=$idref]"/>
      <xsl:variable name="datatype">
        <xsl:apply-templates select="$datatypenode"/>
      </xsl:variable>
      <xsl:variable name="length">
        <xsl:apply-templates select="$datatypenode" mode="length"/>
      </xsl:variable>
      <typing:property type="{$datatype}">
        <xsl:if test="$length != ''">
          <xsl:attribute name="length">
            <xsl:value-of select="$length"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="@changeability = 'frozen'">
          <xsl:attribute name="readOnly">true</xsl:attribute>
        </xsl:if>
      </typing:property>
      <!--
      </xsl:otherwise>
      </xsl:choose>-->
    </xsl:element>
  </xsl:template>

  <xsl:template mode="Inherited" match="UML:Attribute">
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:element name="{$qname}">
      <xsl:variable name="idref" select="UML2:TypedElement.type/UML:DataType/@xmi.idref"/>
      <xsl:variable name="datatype">
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:DataType[@xmi.id=$idref]"/>
      </xsl:variable>
      <xsl:variable name="length">
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:DataType[@xmi.id=$idref]" mode="length"/>
      </xsl:variable>
      <typing:property type="{$datatype}" inherited="true">
        <xsl:if test="$length != ''">
          <xsl:attribute name="length">
            <xsl:value-of select="$length"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="@changeability = 'frozen'">
          <xsl:attribute name="readOnly">true</xsl:attribute>
        </xsl:if>
      </typing:property>
    </xsl:element>
  </xsl:template>

  <xsl:template mode="Interface" match="UML:Attribute">
    <xsl:variable name="qname" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:element name="{$qname}">
      <xsl:variable name="idref" select="UML2:TypedElement.type/UML:DataType/@xmi.idref"/>
      <xsl:variable name="datatype">
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:DataType[@xmi.id=$idref]"/>
      </xsl:variable>
      <xsl:variable name="length">
        <xsl:apply-templates select="/XMI/XMI.content/UML:Model//UML:DataType[@xmi.id=$idref]" mode="length"/>
      </xsl:variable>
      <typing:property type="{$datatype}">
        <xsl:if test="$length != ''">
          <xsl:attribute name="length">
            <xsl:value-of select="$length"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="@changeability = 'frozen'">
          <xsl:attribute name="readOnly">true</xsl:attribute>
        </xsl:if>
      </typing:property>
    </xsl:element>
  </xsl:template>

  <xsl:template match="UML:AssociationEnd">
    <xsl:if test="not(@isNavigable='false')">
      <xsl:variable name="idref" select="UML:AssociationEnd.participant/UML:*/@xmi.idref"/>
      <xsl:variable name="name">
        <xsl:apply-templates mode="Role" select="."/>
      </xsl:variable>
      <xsl:element name="{$name}">
        <xsl:variable name="type" select="/XMI/XMI.content/UML:Model//UML:*[@xmi.id = $idref]"/>
        <xsl:variable name="aggregate">
          <xsl:value-of select="@aggregation!='none'"/>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$aggregate">
            <typing:reference type="{$type}" aggregate="true"/>
          </xsl:when>
          <xsl:otherwise>
            <typing:reference type="{$type}"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template name="Namespace">
    <xsl:param name="component"/>
    <xsl:variable name="id" select="@xmi.id"/>
    <xsl:variable name="namespacedotted" >
      <xsl:for-each select="ancestor-or-self::*">
        <xsl:if test="name() = 'UML:Package'">
          <xsl:value-of select="concat(@name,'.')"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="namespace" select="substring($namespacedotted,1,string-length($namespacedotted) - 1)"/>
    <!--<xsl:message><xsl:value-of select="$namespace"/></xsl:message>
	<xsl:message><xsl:value-of select="concat($component, '.')"/></xsl:message>
	<xsl:message><xsl:value-of select="substring($namespace,string-length(concat($component, '.'))+1)"/></xsl:message>-->
    <xsl:choose>
      <xsl:when test="starts-with($namespace, concat($component/@name, '.'))">
        <xsl:value-of select="substring($namespace,string-length(concat($component/@name, '.'))+1)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="namespace"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="UML:Class|UML:Interface" mode="FullType">
    <xsl:param name="component"/>
    <xsl:variable name="namespace">
      <xsl:call-template name="Namespace">
        <xsl:with-param name="component" select="$component"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="componentName" >
      <xsl:call-template name="ComponentName">
        <xsl:with-param name="component" select="$component"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="type" select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
    <xsl:choose>
      <xsl:when test="string-length($namespace) > 0">
        <xsl:value-of select="$componentName"/>.<xsl:value-of select="$namespace"/>.<xsl:value-of select="$type"/>, <xsl:value-of select="$componentName"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$componentName"/>.<xsl:value-of select="$type"/>, <xsl:value-of select="$componentName"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="UML:AssociationEnd" mode="Role">
    <xsl:variable name="idref" select="UML:AssociationEnd.participant/UML:*/@xmi.idref"/>
    <xsl:variable name="class" select="/XMI/XMI.content/UML:Model//UML:*[@xmi.id = $idref]"/>
    <xsl:choose>
      <xsl:when test="@name and @name!=''">
        <xsl:value-of select="env:QName(@name)" xmlns:env="urn:xmd-environment"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="env:QName($class/@name)" xmlns:env="urn:xmd-environment"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="UML:AssociationEnd" mode="multiplicity">
    <xsl:variable name="range" select="UML:AssociationEnd.multiplicity/UML:Multiplicity/UML:Multiplicity.range/UML:MultiplicityRange"/>
    <xsl:choose>
      <xsl:when test="not($range/@upper)">1</xsl:when>
      <xsl:when test="$range/@upper = 1">1</xsl:when>
      <xsl:otherwise>*</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="Features">
    <xsl:param name="class"/>
    <xsl:param name="inherited"/>
    <xsl:param name="interface"/>
    <xsl:param name="component" select="/XMI/XMI.content/UML:Model//UML:Component[UML:Component.residentElement/UML:ElementResidence/UML:ElementResidence.resident/UML:*/@xmi.idref = $class/@xmi.id]"/>

    <xsl:for-each select="$class">
      <xsl:variable name="id" select="@xmi.id"/>
      <xsl:variable name="parentref" select="/XMI/XMI.content/UML:Generalization[UML:Generalization.child/UML:Class/@xmi.idref = $id]/UML:Generalization.parent/UML:Class/@xmi.idref"/>
      <xsl:if test="$parentref">
        <xsl:call-template name="Features">
          <xsl:with-param name="class" select="/XMI/XMI.content/UML:Model//UML:Class[@xmi.id = $parentref]"/>
          <xsl:with-param name="inherited" select="true()"/>
          <xsl:with-param name="component" select="$component"/>
        </xsl:call-template>
      </xsl:if>
      <xsl:variable name="interfaceref" select="/XMI/XMI.content/UML:Model/UML:Abstraction[@client = $id]/@supplier"/>
      <xsl:if test="$interfaceref">
        <xsl:call-template name="Features">
          <xsl:with-param name="class" select="/XMI/XMI.content/UML:Model//UML:Interface[@xmi.id = $interfaceref]"/>
          <xsl:with-param name="interface" select="true()"/>
          <xsl:with-param name="component" select="$component"/>
        </xsl:call-template>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="$inherited">
          <xsl:apply-templates mode="Inherited" select="UML:Classifier.feature/UML:Attribute"/>
        </xsl:when>
        <xsl:when test="interface">
          <xsl:apply-templates mode="Interface" select="UML:Classifier.feature/UML:Attribute"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="UML:Classifier.feature/UML:Attribute"/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:for-each select="/XMI/XMI.content/UML:Model//UML:Association/UML:Association.connection/UML:AssociationEnd[UML:AssociationEnd.participant/UML:Class/@xmi.idref=$id]">
        <xsl:variable name="adjacentid" select="@xmi.id"/>
        <xsl:variable name="isNavigable" select="not(@isNavigable='false')"/>
        <xsl:variable name="adjacentRole">
          <xsl:apply-templates mode="Role" select="."/>
        </xsl:variable>
        <xsl:variable name="incidentRole" select="../UML:AssociationEnd[@xmi.id != $adjacentid]"/>
        <xsl:if test="not($incidentRole/@isNavigable='false')">
          <xsl:variable name="idref" select="$incidentRole/UML:AssociationEnd.participant/UML:*/@xmi.idref"/>
          <xsl:variable name="name">
            <xsl:apply-templates mode="Role" select="$incidentRole"/>
          </xsl:variable>
          <xsl:element name="{$name}">
            <xsl:variable name="referenceClass" select="/XMI/XMI.content/UML:Model//UML:*[@xmi.id = $idref]"/>

            <xsl:variable name="referencetype">
              <xsl:apply-templates mode="FullType" select="$referenceClass">
                <xsl:with-param name="component" select="$component"/>
              </xsl:apply-templates>
            </xsl:variable>
            <xsl:variable name="multiplicity">
              <xsl:apply-templates mode="multiplicity" select="$incidentRole"/>
            </xsl:variable>
            <xsl:variable name="adjacentMultiplicity">
              <xsl:apply-templates mode="multiplicity" select="."/>
            </xsl:variable>
            <xsl:choose>
              <!-- don't use <xsl:attribute> otherwise generated <typing:reference> is split into begin and end tags-->
              <xsl:when test="$isNavigable and position() = 0 and $inherited">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" mainrole="true" inherited="true"/>
              </xsl:when>
              <xsl:when test="$isNavigable and $inherited">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" inherited="true"/>
              </xsl:when>
              <xsl:when test="$inherited">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" inherited="true"/>
              </xsl:when>

              <xsl:when test="$isNavigable and position() = 0 and $interface">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" mainrole="true" interface="true"/>
              </xsl:when>
              <xsl:when test="$isNavigable and $interface">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" interface="true"/>
              </xsl:when>
              <xsl:when test="$interface">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" interface="true"/>
              </xsl:when>

              <xsl:when test="$isNavigable and position() = 0">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" mainrole="true"/>
              </xsl:when>
              <xsl:when test="$isNavigable">
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}"/>
              </xsl:when>
              <xsl:otherwise>
                <typing:reference navigable="{$isNavigable}" type="{$referencetype}" target="{$referenceClass/@name}" multiplicity="{$multiplicity}" adjacentMultiplicity="{$adjacentMultiplicity}" parent="{$adjacentRole}" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:element>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="GetStereotypes">
    <xsl:param name="ObjectID"/>
    <xsl:variable name="Result">
      [<xsl:for-each select="ancestor-or-self::UML:Package//UML:Stereotype">
        <xsl:variable name="elements" select="concat(';', translate(@extendedElement,' ',';'),';')"/>
        <xsl:if test="contains($elements, concat(';', $ObjectID, ';'))">
          <xsl:value-of select="@name"/>,
        </xsl:if>
      </xsl:for-each>]
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$Result ='[]'"></xsl:when>
      <xsl:when test="contains($Result, ',]')">
        <xsl:text>
</xsl:text>
        <xsl:value-of select="concat(substring-before($Result,',]'),']')" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>
</xsl:text>
        <xsl:value-of select="$Result" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>